import { request } from "@/request";
import { InputData, LayoutData } from "@/types";
import { AxiosRequestConfig } from "axios";
import { TaskDetailRes } from "./tasks";

enum Action {
  Add = 'add',
  Del = 'del',
  Update = 'update',
  List = 'list',
  Detail = 'detail'
}
enum ActionMethod {
  Add = 'post',
  Del = 'delete',
  Update = 'put',
  List = 'get',
  Detail = 'get',
}
interface ApiConfig extends Record<Action, Required<Pick<AxiosRequestConfig, 'url' | 'method'>>> { }

export interface OrderData extends Array<OrderDataItem> { }
export type TaskData = TaskDetailRes
export interface OrderDataItem {
  id: string;
  date: number;
  source: Source;
  process: Process;
  ratio: number;
  count: number;
  time: number;
  taskData?: TaskData;
  designName?: string;
}
export interface OrderDetail { designName: string, inputData: InputData, layoutDatas: LayoutData[] }
export enum Source {
  Import,
  ThirdOrder,
  CurrentDesign,
}
export enum Process {
  Wait,
  Finish
}

const baseUrl = '/order'

const apiConfig: ApiConfig = {
  [Action.Add]: { url: baseUrl + '/' + Action.Add, method: ActionMethod.Add },
  [Action.Del]: { url: baseUrl + '/' + Action.Del, method: ActionMethod.Del },
  [Action.Update]: { url: baseUrl + '/' + Action.Update, method: ActionMethod.Update },
  [Action.List]: { url: baseUrl + '/' + Action.List, method: ActionMethod.List },
  [Action.Detail]: { url: baseUrl + '/' + Action.Detail, method: ActionMethod.Detail },
}
export { apiConfig as orderApiConfig };

export async function getOrderList() {
  return (await request(apiConfig.list)).data
}
export async function delOrder(id: string) {
  return (await request({ ...apiConfig.del, data: { id } })).data
}
export async function getOrderDetail(id: string) {
  return (await request({ ...apiConfig.detail, params: { id } })).data
}
export async function addOrder(data) {
  console.log('addOrder', { ...apiConfig.add, data });
  return (await request({ ...apiConfig.add, data })).data
}
export async function updateOrder(data) {
  console.log('updateOrder', { data });
  return (await request({ ...apiConfig.update, data })).data
}